package com.freesun.shop.controller;

import com.freesun.shop.domin.ProdSolr;
import com.freesun.shop.entity.Category;
import com.freesun.shop.service.CategoryService;
import com.freesun.shop.service.SearchService;
import com.freesun.shop.vo.CategoryVo;
import ma.glasnost.orika.MapperFacade;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CategoryController {

    @Reference(check = false)
    private CategoryService categoryService;

    @Autowired
    private MapperFacade mapperFacade;

    @Reference(check = false)
    private SearchService searchService;

    @GetMapping("/category/categoryInfo")
    public ResponseEntity<List<CategoryVo>> listCategory(@RequestParam(defaultValue = "0") Long parentId) {
        List<Category> categories=  categoryService.getSubNodes(parentId);
        List<CategoryVo> categoryVos = mapperFacade.mapAsList(categories, CategoryVo.class);
        return ResponseEntity.ok(categoryVos);
    }

    @GetMapping("/prod/pageProd")
    public ResponseEntity<List<ProdSolr>> serachByCatId(Long categoryId) {
        List<ProdSolr> prodSolrs = searchService.serachByCatId(categoryId);
        return ResponseEntity.ok(prodSolrs);
    }
}
